package com.tinshow.web.httpclient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tinshow.config.mybatis.SpringContextHolder;
import com.tinshow.config.utils.WebUtils;
import com.tinshow.model.article.Music;
import com.tinshow.service.index.MusicService;
import com.tinshow.service.index.impl.MusicServiceImpl;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by libo4 on 2017/5/29.
 */
public class EachClient {

    private static long startTime = System.currentTimeMillis();

    public static List<Music> getEchoIndex(){
        String uri = "http://www.app-echo.com/api/other/index";
        Map<String,String> reqParmMap = new HashMap<>();
        reqParmMap.put("Accept","application/json, text/plain, */*");
        reqParmMap.put("Accept-Encoding","gzip, deflate, sdch");
        reqParmMap.put("Accept-Language","zh-CN,zh;q=0.8");
        reqParmMap.put("Host","www.app-echo.com");
        reqParmMap.put("Referer","http://www.app-echo.com/");
        reqParmMap.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2759.400 QQBrowser/9.6.11205.400");
        GetMethod getMethod = (GetMethod) WebUtils.doGet(uri,reqParmMap);
        try {
            if(getMethod != null){
                String result = WebUtils.getResponseBody4Zip(getMethod);
                if(result != null){
                    List<Music> list = parseJson(result);
                    return list;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<Music> parseJson(String result){
        System.out.println(result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray jsonArray = jsonObject.getJSONArray("hot_recommend");
        List<Music> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject hotRecomment = jsonArray.getJSONObject(i);
            JSONObject songInfo = hotRecomment.getJSONObject("song_info");
            String songer = "";
            if(songInfo != null){
                songer = songInfo.getJSONObject("author").getString("name");
            }
            String name = hotRecomment.getString("name");
            String source = hotRecomment.getString("source");
            String pic = hotRecomment.getString("pic");
            System.out.println(name);
            Music music = new Music();
            music.setAuthor(songer);
            music.setName(name);
            music.setPicUrl(pic);
            music.setUrl(source);
            list.add(music);
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime + "ms");
        return list;
    }

    public static void main(String[] args){
        getEchoIndex();
    }

}
